import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class JavaTime {
    public  static void main(String[] args)
    {
            //时间api
            Instant start=Instant.now();
            runAlgorithm();
            Instant end=Instant.now();
            Duration timeElapsed=Duration.between(start,end);
            long millis=timeElapsed.toMillis();
            System.out.printf("%d milliseconds\n", millis);
            Instant start2=Instant.now();
            runAlgorithm2();
            Instant end2=Instant.now();
            Duration timeElapsed2=Duration.between(start,end);
            long millis2=timeElapsed.toMillis();
            System.out.printf("%d milliseconds\n", millis2);

            //日期api
            LocalDate today=LocalDate.now();
            System.out.println(today);
            LocalDate birthday=LocalDate.of(1999,11,17);
            System.out.println(birthday);
            LocalDate a=LocalDate.of(2021,1,1).plusDays(20);
            System.out.println(a);
            LocalDate independenceday=LocalDate.of(2021, Month.OCTOBER,1);
            LocalDate christmas=LocalDate.of(2021,Month.DECEMBER,25);
            System.out.println("Until christmas:"+independenceday.until(christmas));
            System.out.println("Until christmas:"+independenceday.until(christmas, ChronoUnit.DAYS));
            LocalDate start1=LocalDate.of(2000,1,1);
            LocalDate end1=LocalDate.now();
            Stream<LocalDate> firstdayinmonth=start1.datesUntil(end1, Period.ofMonths(1));
            System.out.println("Firstdayinthemonth:"+firstdayinmonth.collect(Collectors.toList()));
    }
    public static void runAlgorithm()
    {
        int size=10;
        List<Integer> list=new Random().ints().map(i->i%100).limit(size).boxed().collect(Collectors.toList());
        Collections.sort(list);
        System.out.println(list);
    }
    public static void runAlgorithm2()
    {
        int size=10;
        List<Integer> list=new Random().ints().map(i->i%100).limit(size).boxed().collect(Collectors.toList());
        while(!IntStream.range(1,list.size()).allMatch(i->list.get(i-1).compareTo(list.get(i))<=0))
        {
            Collections.shuffle(list);
        }
        System.out.println(list);
    }
}
